
using System;
using System.Collections;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;

using CStock;


namespace CStock
{
	
	/// <summary>
	/// Summary description for Traders.
	/// </summary>
	public class Traders : StockObjectCollection , ITraders
	{
		#region Constructor
        public Traders()
            : base()
		{

            ResetPathFromConfiguration();
            //this.Orders = new Orders();
		}

        public override bool ResetPathFromConfiguration()
        {
           
            ProgramContext pc = ProgramContext.Instance;
            Debug.Assert(pc != null);
            if (pc != null)
                this.path = pc.Configuration.Directories.TradersPath;
            return true;

        }
		#endregion	

		#region Class ArrayList Override
		
		

		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public int Add(object obj)
		{
            Trader newTrader = (Trader)obj;
			if (this.items.Count<=0)
			{
				this.items.Add(newTrader);
				//OnChanged(EventArgs.Empty);
				return 0;
			}
			else
			{
				if (!this.items.Contains(newTrader))
				{
					int index = this.items.Count;
					for (int i = 0;i < this.items.Count;i++)
					{
						Trader listTrader=(Trader)this[i];
						if ((newTrader.CompareTo(listTrader)<0))
						{
							index=i;
							break;
						}
					}
					this.items.Insert(index,newTrader);
					//OnChanged(EventArgs.Empty);
					return index;
				}
				else
				{
					Debug.Assert(false,"CTraders","Error adding Trader");
					return -1;
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public void Add(ITrader obj)
		{
			this.items.Add((object)obj);
			
		}
		public void Remove(object obj)
		{
			Trader removeTrader=(Trader)obj;
			if (this.items.Contains(removeTrader))
			{
				this.items.Remove(removeTrader);
				//OnChanged(EventArgs.Empty);
			}
		}

		public int IndexOf(object obj)
		{
			return this.items.IndexOf(obj);
		}
		#endregion



		#region Implementation
		
		/// <summary>
		/// 
		/// </summary>
        public new ITrader this[string myid]
		{
			get
			{
				for(int i = 0; i < this.items.Count; i ++)
				{
					ITrader myPf = this[i]  as ITrader;
					if( myPf.ID == myid)
						return myPf;
				}
				return null;
			}
		}
        public   new ITrader this[int index]
		{
			get
			{
				try
				{
					if ((index >= 0) && (index < this.items.Count))
					{
						object o = base[index];
						return o as ITrader;
					}
					else
					{
						return null;
					}
				}
				catch
				{
					Debug.Assert(false,"Traders : Error getting Trader");
					return null;
				}
			}
			set
			{
				if ((index >= 0) && (index < this.items.Count))
				{
					base[index] = value;
				}
			}
		}

		#endregion


		/// <summary>
		/// ToTraderItemArray
		/// </summary>
		/// <returns></returns>
		public DataItem[] ToTraderItemArray()
		{
			try
			{
				return (DataItem[])this.items.ToArray(typeof(DataItem));
			}
			catch (Exception exc)
			{
				Debug.WriteLine("DataItems - Error creating TraderItemArray" + exc.Message);
			}
			return null;
		}
        public new bool Serialize()
        {
            ResetPathFromConfiguration();
            for (int i = 0; i < this.items.Count; i++)
            {
                ITrader trad = this[i];
                trad.ResetPathFromConfiguration();
                trad.Serialize();

            }
            if (this.items.Count <= 0)
            {
                System.IO.Directory.CreateDirectory(this.path);
            }
            return true;

        }
        public new bool DeSerialize()
        {
            ProgramContext dh = ProgramContext.Instance;
            string myPath = dh.Configuration.Directories.TradersPath;



            return DeserializeTraders(myPath);
        }
        protected bool DeserializeTraders(string myPath)
        {
            //search for all portfolios:
            if (!System.IO.Directory.Exists(myPath))
                System.IO.Directory.CreateDirectory(myPath);

            //-------------------------------------------
            //search for files in that dir
            System.IO.DirectoryInfo di = new DirectoryInfo(myPath);
            
            foreach (FileInfo file in di.GetFiles("*.xml"))
            {

                string strFile = file.Name;
                string idTrader = CUtil.IOUtil.ExtractNameFromFileName(strFile);



                ITrader trad = new Trader();
                trad.ID = idTrader;
                //the tradeEconomy is also a trader - do not add this one    

                //if (trad.ID != this.iD)
                {

                    object o = new Trader();

                    if (trad.DeSerialize(ref o))
                    {
                        trad = o as Trader;

                        this.Add(trad);
                    }
                }

            }
            return true;
        }
	}
}
